;
$(function() { 
	var startDateTime;
	var endDateTime;
	var queueIP;
	function getData(endDate) {
		//console.log("getData involed: -->" + $("#ajaxUrl").val()) + "?jmxUrl=" + $("#jmxUrl").val() + "&dateStart=" + $("#datetimStart").val()
		var dataPend = {"jmxUrl" : $("#jmxUrl").val() ,"dateStart": startDateTime, "queueIP":queueIP };
		if(endDate){
			dataPend = {"jmxUrl" : $("#jmxUrl").val() ,"dateStart": startDateTime, "dateEnd":endDate, "queueIP":queueIP };
		}
		$.ajax({
			type : 'get',
			url : $("#ajaxUrl").val() + "/queue/getListQueueData",
			data: dataPend,
			success : function(result) {
				if( result ){
					//$("#queueLineChart").css({ height: "500px"});
					var queueOpts = performance.getListQueueLineOpts(result);
					//console.log(result);
					myQueueChart.setOption(queueOpts);
				}else{
					//$("#queueLineChart").css({ height: "0px"});
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				console.log("some error occured" + XMLHttpRequest);
				console.log("Actually, there is a error, but I don't know what it realy is? so, I call it the unknow error");
                //alert(XMLHttpRequest.status);
                //alert(XMLHttpRequest.readyState);
                //alert(textStatus);
            }
		});
		
		$.ajax({
			type : 'get',
			url : $("#ajaxUrl").val() + "/getDynamicData",
			data: dataPend,
			success : function(result) { 
				loading.hideLoadingIcon("#loadingIcon"); 
				if(result){
					var lineOpts = performance.getSummaryLineOptions(result);
					myLineChart.setOption(lineOpts);
					var heapOpts = performance.getHeapLineOptions(result);
					myHeapChart.setOption(heapOpts);
					if(!endDate){						
						var panelOpts = performance.getPanelOptions(result);
						myPanelChart.setOption(panelOpts);
					}
				} 
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				console.log("some error occured" + XMLHttpRequest);
				console.log("Actually, there is a error, but I don't know what it realy is? so, I call it the unknow error")
                //alert(XMLHttpRequest.status);
                //alert(XMLHttpRequest.readyState);
                //alert(textStatus);
            }
		});
	};
	
	var intervalTime = 15; 
	var jmxUrlText = $("#jmxUrl").val();
	var canStart = true;
	function countTime() {
		//console.log("countTime involved");
		if (!intervalTime) {
			intervalTime = 15;
		} else {
			intervalTime = $("#interval").val() - 0;
		} 
		if (!jmxUrlText) {
			alert("The jmxurl has an invalid value!");
			canStart = false;
		}
		if (canStart) {
			getData()
		}
	}
	var myLineChart = null;
	var myQueueChart = null;
	var myHeapChart =null;
	var myPanelChart =null;
	function initChart(){
		myLineChart = performance.initEchart("#summaryLineChart");
		myHeapChart = performance.initEchart("#heapChart");
		myPanelChart = performance.initEchart("#panelChart"); 
		var queueChar = $("#queueLineChart");
		if(queueChar.length > 0){ 
			myQueueChart = performance.initEchart("#queueLineChart"); 
		}
	}

	//不要每次都重新获取datetimepicker里面的时间，等点击start按钮的时候再获取，不然时间一选择好久回马上刷新chart。
	$("#startReport").on("click",function(){ 
		loading.showLoadingIcon("#loadingIcon"); 
		//console.log("startReport clicked" );
		startDateTime = $("#datetimStartInput").val();
		queueIP = $("#queueIP").val();
		if(startDateTime){
			
			if(flushtimer){
				clearInterval(flushtimer);
			}
			initChart();
			getData();// 立即执行一次
			loading.showLoadingIcon("#loadingIcon");
			var flushtimer = setInterval(countTime, parseInt(intervalTime) * 1000); // 不停的，循环调用
			//setTimeout(countTime,countTime,intervalTime * 1000); //只执行一次
		}else{
			alert("Something is wrong with the time picker ");
			loading.hideLoadingIcon("#loadingIcon"); 
		}
	})
	
	$("#showReport").on("click",function(){
		loading.showLoadingIcon("#loadingIcon");
		//setTimeout(initChart,1000); 
		//console.log("startReport clicked" );
		initChart();
		startDateTime = $("#datetimStartInput").val()
		endDateTime = $("#datetimEndInput").val();
		queueIP = $("#queueIP").val();
		if(startDateTime && endDateTime){
			getData(endDateTime);// 立即执行 
			loading.showLoadingIcon("#loadingIcon");
			//var flushtimer = setInterval(countTime, parseInt(intervalTime) * 1000); // 不停的，循环调用
			//setTimeout(countTime,countTime,intervalTime * 1000); //只执行一次			
		}else{
			alert("Something is wrong with the time picker ");
			loading.hideLoadingIcon("#loadingIcon"); 
		}
	});
	
	$("#reportExport").on("click",function(){
		//showLoadingIcon("#loadingIcon");
		//console.log("startReport clicked" );
		startDateTime = $("#datetimStartInput").val();
		endDateTime= $("#datetimEndInput").val();
		queueIP = $("#queueIP").val();
		var opUrl = $("#ajaxUrl").val() + "/exportReport2File?dateStart=" + startDateTime + 
		"&dateEnd=" + endDateTime + "&jmxUrl=" + $("#jmxUrl").val();
		window.open(opUrl);
		
		//getData(dateEnd);// 立即执行 
		//var flushtimer = setInterval(countTime, parseInt(intervalTime) * 1000); // 不停的，循环调用
		//setTimeout(countTime,countTime,intervalTime * 1000); //只执行一次
	});
	
	
})